#include "Elemental.h"
#include "Entity.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "Game.h"
#include "GameplayState.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "Waypoint.h"
#include "Player.h"
#include "ObjectManager.h"
#include "MessageSystem.h"
#include "Message.h"
#include "DestoryElementalMessage.h"
#include "Avatar.h"
#include "Frame.h"
#include "AnimationManager.h"
#include "AnimationInfo.h"
#include "TargetBox.h"
#include "ObjectManager.h"
#include "GameplayState.h"

CElemental::CElemental(void)
{
	m_pAnimManager = CAnimationManager::GetInstance();
	m_pCurrAnimInfo.SetCurrentAnimation( "PC_PlayerWalkLeft" );


	TypeofObj = Obj_Elemental;
	this->m_vQueueSouls.push_back(0);
	this->m_vQueueSouls.push_back(0);
	this->m_vQueueSouls.push_back(0);
	this->m_vQueueSouls.push_back(0);
	m_bCalculateVec = true;	

	m_pTarget = nullptr;
	attackTimer = 0;
	targetDeleted = false;
	m_bResetVec = false;
	showExtraDamage = false;
	m_bHasTarget = false;
}


CElemental::~CElemental(void)
{
	

}

void CElemental::Update( float fElapsedTime )
{
	CGameplayState::GetInstance()->CheckForTargets(this);
	//Calculate vector direction
	if( m_bCalculateVec == true || m_bResetVec == true)
	{
	tVector2D walkVec;
	walkVec.fX = m_pWaypoint->GetPosX() - this->GetPosX();
	walkVec.fY = m_pWaypoint->GetPosY() - this->GetPosY();

	//Normalize
	float fLength = sqrt((walkVec.fX * walkVec.fX) + (walkVec.fY * walkVec.fY));

	//Multiple by Move Speed, this should be a float having 1 be base movement speed
	
	walkVec.fX = walkVec.fX / fLength;
	walkVec.fY = walkVec.fY / fLength;

	
	SetVelX(walkVec.fX*100);
	SetVelY(walkVec.fY*100);
	m_bCalculateVec = false;
	m_bResetVec = false;
	}

	m_pAnimManager->Update( &m_pCurrAnimInfo, fElapsedTime );	if( m_pTarget != nullptr )
	{
		SetVelX(0);
		SetVelY(0);
		attackTimer += fElapsedTime;
		if( attackTimer >= GetAttackSpeed() )
		{
			DealDamage(m_pTarget);
			attackTimer = 0;
		}
	}


	CEntity::Update(fElapsedTime);
}

void CElemental::Render( void )
{
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	//Replace with the animation engine
	CSGD_TextureManager::GetInstance()->Draw(GetImageID(), (int)(GetPosX()), (int)(GetPosY()),
		2,2);
m_pAnimManager->Render( &m_pCurrAnimInfo, this->GetPosX(), this->GetPosY(), false, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );if( showExtraDamage == true )
	{
	RECT rShow;
	rShow.top = (LONG)(GetPosY() - 30);
	rShow.left = (LONG)(GetPosX() - 30);
	rShow.right = (LONG)(GetPosX() + 30);
	rShow.bottom = (LONG)(GetPosY() + 30);
	if( this->GetTeam() == 1 && showExtraDamage == true)
	CSGD_Direct3D::GetInstance()->DrawRect(rShow, 0,255,0);
	else if( this->GetTeam() == 2 && showExtraDamage == true)
	CSGD_Direct3D::GetInstance()->DrawRect(rShow, 0,0,255);
	}

	if( CEntity::GetRange() > 0 )
	CSGD_Direct3D::GetInstance()->DrawRect( m_pRangeBox->GetSize(), 255,0,0);}


bool CElemental::CheckCollision( IEntity* pOther )
{
	if( CEntity::CheckCollision(pOther) != true )
	{
		return false;
	}
	switch( pOther->GetType() )
	{
		case Obj_Elemental:
			{
				if( m_pOwner != dynamic_cast<CElemental*>(pOther)->GetOwner() && this->GetLane() == dynamic_cast<CElemental*>(pOther)->GetLane())
				{
					if( this->GetRange() <= 0 && m_bHasTarget == false)
					{
						m_pTarget = dynamic_cast<CEntity*>(pOther);
						m_pTarget->AddRef();
						m_bHasTarget = true;
					}
				return true;
				}
			}
		case Obj_Waypoint:
			{
				if( pOther == m_pWaypoint )
				{
					if( m_pOwner->GetPlayer() == true )
					{
						if( m_pWaypoint->GetNext() != nullptr )
							m_pWaypoint = m_pWaypoint->GetNext();
					}
					else if( m_pOwner->GetPlayer() == false )
					{
						if( m_pWaypoint->GetPrev() != nullptr )
							m_pWaypoint = m_pWaypoint->GetPrev();
					}
					m_bCalculateVec = true;
				return true;
				}
				return false;
			}
		case Obj_Avatar:
			{
				if( GetTeam() != dynamic_cast<CAvatar*>(pOther)->GetTeam() )
				{
					m_pOwner->IncreasePool(m_vQueueSouls);
					CDestoryElementalMessage* pMSG = new CDestoryElementalMessage(dynamic_cast<CElemental*>(this));
					CAvatar* pAvatar = dynamic_cast<CAvatar*>(pOther);
						//m_pCurrFrame.GetMessageA() == "DestroyElemental")
					//if( pAvatar->GetAnimation().m_vFrames[ pAvatar->GetAnimationInfo().GetCurrentFrame()]->GetMessageA() == "DestroyElemental" )
					//if(pAvatar->)
					//{
					if(m_pAnimManager->m_pFrame.GetMessageA() == "DestroyElemental" )
					{
						CMessageSystem::GetInstance()->SendMsg(pMSG);
					}
				
					//}
				}
			}
	}
	return false;
}

void CElemental::DealDamage( CEntity* target )
{
	short bonus = 0;
	//fire
	if( m_vQueueSouls[Fire] != 0 )
		{
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Air) )
				++bonus;
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Water) )
				--bonus;
		}
		if(  m_vQueueSouls[Air] != 0  )
		{
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Earth) )
				++bonus;
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Fire) )
				--bonus;
		}
		if(  m_vQueueSouls[Earth] != 0  )
		{
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Water) )
				++bonus;
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Air) )
				--bonus;
		}
		if( m_vQueueSouls[Water] != 0  )
		{
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Fire) )
				++bonus;
			if( dynamic_cast<CElemental*>(target)->GetSingleSoul(Earth) )
				--bonus;
		}
		if( bonus > 0 )
			showExtraDamage = true;
	target->SetHealth((short)(target->GetHealth()) - ((short)(this->GetAttackDamage()) - ((short)(bonus*5))));
	if( target->GetHealth() <= 0 )
	{
			m_pOwner->IncreasePool(m_vQueueSouls);
			CDestoryElementalMessage* pMSG = new CDestoryElementalMessage(dynamic_cast<CElemental*>(target));
			CMessageSystem::GetInstance()->SendMsg(pMSG);
			m_pTarget->Release();
			m_pTarget = nullptr;
			targetDeleted = true;
			m_bResetVec = true;
			showExtraDamage = false;
			m_bHasTarget = false;
	}
}

void CElemental::SetTargetBox( int nX, int nY, short range )
{
		//Super hard coded because of no anchor points
	//m_pRangeBox->SetUpBox( nX, nY, range );
}

void CElemental::CheckForTarget( int laneNum )
{
	
}